home *** CD-ROM | disk | FTP | other *** search
- Path: gsusgi1.gsu.edu!not-for-mail
- From: matmrlx@gsusgi1.gsu.edu (Michael R. Lauer)
- Newsgroups: comp.lang.c
- Subject: free space malloced to auto vars??
- Date: 1 Feb 1996 08:29:58 -0500
- Organization: Georgia State University
- Message-ID: <4eqf8m$smv@gsusgi1.gsu.edu>
- NNTP-Posting-Host: gsusgi1.gsu.edu
- Summary: Do I free space malloced to auto vars on leaving scope?
- Keywords: malloc
- X-Newsreader: NN version 6.5.0 #3 (NOV)
-
- The general question is: if you malloc space for a pointer with automatic
- storage, should/must you free that space before leaving that scope?
-
- This seems obvious but it is giving me trouble. In this code:
-
- {
- struct dirent *dp;
- if ((dp = readdir() != NULL)
- {
- }
- if (dp) /* this seems to cause a segmentation fault */
- free(dp); /* when the enclosing function returns */
- }
-
- So readdir returns a pointer to the dirent structure--it is mallocing the
- space. When I leave this scope I AM done with dp and the struct.
- Don't I have to free(dp)? The "if(dp) free(dp)" at the end of the scope caused
- a segmentation fault when I returned from the enclosing function. It
- apparently works fine without the free(), but I am worried about memory
- leakage.
-
-